﻿using System;
using System.Collections.Generic;
using System.Configuration;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace ZoneLineNew.Core
{
    public class LogManager {
        private static readonly object lockHelper = new object();
        private static readonly string loggerName = "NLogger";

        private static readonly NLog.Logger NLogLogger;
        static LogManager() {
            lock (lockHelper) {
                if (NLogLogger == null) {
                    NLogLogger = NLog.LogManager.GetLogger(loggerName);
                }
            }
        }

        /// <summary>
        /// 输出Info级日志
        /// </summary>
        /// <param name="info">日志信息</param>
        public static void Info(string info, params object[] paras) {
            try {
                StringBuilder stringBuilder = new StringBuilder();
                //stringBuilder.Append("\r\n");
                //int lineNumber = new StackTrace(true).GetFrame(1).GetFileLineNumber();
                //string fileName = new StackTrace(true).GetFrame(1).GetFileName();
                //stringBuilder.Append($"Position：{fileName} Code Line：{lineNumber}");
                //stringBuilder.Append("\r\n");
                stringBuilder.Append($" {ConfigurationManager.AppSettings["Version"].ToString()} " + info);
                stringBuilder.Append("\r\n");
                NLogLogger.Info(stringBuilder.ToString(), paras);
            } catch (Exception) { }
        }

        /// <summary>
        /// 输出Error级日志
        /// </summary>
        /// <param name="info">日志信息</param>
        public static void Error(string info) {
            try {
                NLogLogger.Error($" {ConfigurationManager.AppSettings["Version"].ToString()} " + info);
            } catch (Exception) { }
        }

        /// <summary>
        /// 输出Error级日志
        /// </summary>
        /// <param name="info"></param>
        /// <param name="ex"></param>
        public static void Error(Exception ex) {
            try {
                NLogLogger.Error(ex, ex.StackTrace);
            } catch (Exception) { }
        }

        /// <summary>
        /// 输出Error级日志
        /// </summary>
        /// <param name="info"></param>
        /// <param name="ex"></param>
        public static void Error(string info, Exception ex) {
            try {
                NLogLogger.Error(ex, $" {ConfigurationManager.AppSettings["Version"].ToString()} " + info + "|" + ex.StackTrace);
            } catch (Exception) { }
        }

        /// <summary>
        /// 输出Debug级日志
        /// </summary>
        /// <param name="info">日志信息</param>
        public static void Debug(string info, params object[] paras) {
            try {
                NLogLogger.Debug($" {ConfigurationManager.AppSettings["Version"].ToString()} " + info, paras);
            } catch (Exception) { }
        }

        /// <summary>
        /// 输出Trace级日志
        /// </summary>
        /// <param name="info">日志信息</param>
        public static void Trace(string info, params object[] paras) {
            try {
                NLogLogger.Trace($" {ConfigurationManager.AppSettings["Version"].ToString()} " + info, paras);
            } catch (Exception) { }
        }

        /// <summary>
        /// 输出Warn级日志
        /// </summary>
        /// <param name="info">日志信息</param>
        public static void Warn(string info) {
            try {
                NLogLogger.Warn($" {ConfigurationManager.AppSettings["Version"].ToString()} " + info);
            } catch (Exception) { }
        }
    }
}
